﻿/*
* Copyright (c) 2022,
* All rights reserved.
*
* 文件名称：character.h
* 创建日期：2022/4/20
* 摘 要：角色类接口实现
*
* 当前版本：0.1
* 作 者：mason
* 完成日期：
*/

#include "character.h"
#include "ui_character.h"

Character::Character(QString img_path, int img_num, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Character)
{
    ui->setupUi(this);

    /* 窗口属性设置 */
    setAutoFillBackground(false);
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_TranslucentBackground,true);

    qDebug()<<img_path << img_num;
    /* pixmap list init */
    for (int i = 0; i < img_num; i++)
    {
        QString fileName = img_path + \
                (i > 8 ? QString("%1.png").arg(i + 1) \
                      : QString("0%1.png").arg(i + 1));
        qDebug()<<fileName;
        QImage image(fileName);
        pixmap_list.append(QPixmap::fromImage(image));

        //镜像加载
        pixmap_list_mirrored.append(QPixmap::fromImage(image.mirrored(true, false)));
    }


    pixmap_num = img_num;
    fps = 100;
    move_speed = DEFAULT_MOVE_SPEED;
    pixmap_begin = FRAM_RUN_BEGIN;
    pixmap_end = FRAM_RUN_END;
    pixmap_index = pixmap_begin;
    is_pixmap_mirrored = false;
    move_enable = false;    /* 默认关闭键盘移动 */
    time_fd = startTimer(fps);
}



Character::~Character()
{
    delete ui;
}

void Character::setMoveSpeed(int m_speed)
{
    move_speed = m_speed;
}

void Character::setFps(int m_fps)
{
    fps = m_fps;
    killTimer(time_fd);
    time_fd = startTimer(fps);
}

void Character::timerEvent(QTimerEvent *)
{
    pixmap_index++;
    if (pixmap_index >= pixmap_end)
    {
        pixmap_index = pixmap_begin;
    }
    qDebug()<<pixmap_index;
    //    pixmap_index %= pixmap_num;
    repaint();  //窗口重绘
}

void Character::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    if (is_pixmap_mirrored)
    {
        pixmap_list_mirrored[pixmap_index - 1].scaled(this->width(),this->height(), \
                                         Qt::KeepAspectRatio);
        painter.drawPixmap(this->rect(), pixmap_list_mirrored.at(pixmap_index - 1), \
                           pixmap_list_mirrored.at(pixmap_index - 1).rect());
    } else {
        pixmap_list[pixmap_index - 1].scaled(this->width(),this->height(), \
                                         Qt::KeepAspectRatio);
        painter.drawPixmap(this->rect(), pixmap_list.at(pixmap_index - 1), \
                           pixmap_list.at(pixmap_index - 1).rect());
    }

}

void Character::moveRight()
{
    is_pixmap_mirrored = false;
    this->move(this->pos().x() + move_speed, this->pos().y());
}

void Character::moveLeft()
{
    is_pixmap_mirrored = true;
    this->move(this->pos().x() - move_speed, this->pos().y());
}

void Character::moveUp()
{
    this->move(this->pos().x(), this->pos().y() - move_speed / 2);
}

void Character::moveDown()
{
    this->move(this->pos().x(), this->pos().y() + move_speed / 2);
}

void Character::setFrame(int m_begin, int m_end)
{
    pixmap_begin = m_begin;
    pixmap_end = m_end;
    pixmap_index = pixmap_begin;
}

void Character::keyboardMoveSet(bool m_move_enable)
{
    move_enable = m_move_enable;
}

void Character::setFrame(ACTION action)
{
    switch (action) {
    case ACTION_RUN:
        pixmap_begin = FRAM_RUN_BEGIN;
        pixmap_end   = FRAM_RUN_END;
        setFps(FPS_RUN);
        break;
    case ACTION_STAND:
        pixmap_begin = FRAM_STAND_BEGIN;
        pixmap_end   = FRAM_STAND_END;
        setFps(FPS_STAND);
    case ACTION_ATTACK:
        pixmap_begin = FRAM_ATTACK_BEGIN;
        pixmap_end   = FRAM_ATTACK_END;
        setFps(FPS_ATTACK);
    default:
        break;
    }
    pixmap_index = pixmap_begin;
}

void Character::keyPressEvent(QKeyEvent *event)
{
    if (!move_enable)
    {
        return;
    }
    switch (event->key()) {
    case Qt::Key_Up : /* ↑ */

        moveUp();
        break;
    case Qt::Key_Down:  //↓
        moveDown();
        break;


    case Qt::Key_Right: //→
        moveRight();
        break;
    case Qt::Key_Left:
        moveLeft();
        break;

    default:
        break;
    }
}

